/********************************************************************************/
/*   The  Software  is  proprietary,  confidential,  and  valuable to Realtek   */
/*   Semiconductor  Corporation  ("Realtek").  All  rights, including but not   */
/*   limited  to  copyrights,  patents,  trademarks, trade secrets, mask work   */
/*   rights, and other similar rights and interests, are reserved to Realtek.   */
/*   Without  prior  written  consent  from  Realtek,  copying, reproduction,   */
/*   modification,  distribution,  or  otherwise  is strictly prohibited. The   */
/*   Software  shall  be  kept  strictly  in  confidence,  and  shall  not be   */
/*   disclosed to or otherwise accessed by any third party.                     */
/*   c<2003> - <2012>                                                           */
/*   The Software is provided "AS IS" without any warranty of any kind,         */
/*   express, implied, statutory or otherwise.                                  */
/********************************************************************************/

//----------------------------------------------------------------------------------------------------
// ID Code      : ScalerCommonTypeC.c No.0000
// Update Note  :
//----------------------------------------------------------------------------------------------------

#include "ScalerFunctionInclude.h"

#if((_DP_TYPE_C_SUPPORT == _ON) && (_PORT_CONTROLLER_EMBEDDED_SUPPORT == _ON))
//****************************************************************************
// DEFINITIONS / MACROS
//****************************************************************************
#define _TYPEC_DC_OFF_OPTION            _TYPEC_FULL_FUNCTION

//****************************************************************************
// STRUCT / TYPE / ENUM DEFINITTIONS
//****************************************************************************

//****************************************************************************
// CODE TABLES
//****************************************************************************

//****************************************************************************
// VARIABLE DECLARATIONS
//****************************************************************************
//
//****************************************************************************
// FUNCTION DECLARATIONS
//****************************************************************************
void ScalerTypeCHandler(BYTE ucInputPort);
EnumTypeCAttachStatus ScalerTypeCGetCcAttach(BYTE ucInputPort);
EnumTypeCAltModeStatus ScalerTypeCGetAltModeReady(BYTE ucInputPort);
bit ScalerTypeCGetPinAssignment(BYTE ucInputPort, EnumTypeCPinCfgType *penumPinAssignment);
bit ScalerTypeCReconnect(BYTE ucInputPort);
bit ScalerTypeCUpdateConfig(BYTE ucInputPort);
void ScalerTypeCIntHandler_EXINT0(void);
void ScalerTypeCPowerSwitch(EnumPowerAction enumPowerAction);
BYTE ScalerTypeCGetDcOffOption(void);

//****************************************************************************
// FUNCTION DEFINITIONS
//****************************************************************************
//--------------------------------------------------
// Description  : Type C Handler Process
// Input Value  : ucInputPort --> Inputput
// Output Value : None
//--------------------------------------------------
void ScalerTypeCHandler(BYTE ucInputPort)
{
    switch(ucInputPort)
    {
#if(_D0_DP_TYPE_C_PORT_CTRL_TYPE == _PORT_CONTROLLER_EMBEDDED)
        case _D0_INPUT_PORT:

            ScalerTypeCRx0CcHandler();

            break;
#endif

#if(_D1_DP_TYPE_C_PORT_CTRL_TYPE == _PORT_CONTROLLER_EMBEDDED)
        case _D1_INPUT_PORT:

            // ScalerTypeCRx1CcHandler();

            break;
#endif

#if(_D6_DP_TYPE_C_PORT_CTRL_TYPE == _PORT_CONTROLLER_EMBEDDED)
        case _D6_INPUT_PORT:

            // ScalerTypeCRx6CcHandler();

            break;
#endif

        default:

            break;
    }
}

//--------------------------------------------------
// Description  : Return Port Controller CC Attach Status to SysTypeC
// Input Value  : None
// Output Value : CC Attach (_TRUE / _FALSE)
//--------------------------------------------------
EnumTypeCAttachStatus ScalerTypeCGetCcAttach(BYTE ucInputPort)
{
    switch(ucInputPort)
    {
#if(_D0_DP_TYPE_C_PORT_CTRL_TYPE == _PORT_CONTROLLER_EMBEDDED)
        case _D0_INPUT_PORT:

            return GET_CC_RX0_ATTACHED();

            break;
#endif

#if(_D1_DP_TYPE_C_PORT_CTRL_TYPE == _PORT_CONTROLLER_EMBEDDED)
        case _D1_INPUT_PORT:

            // return GET_CC_RX1_ATTACHED();

            break;
#endif

#if(_D6_DP_TYPE_C_PORT_CTRL_TYPE == _PORT_CONTROLLER_EMBEDDED)
        case _D6_INPUT_PORT:

            // return GET_CC_RX6_ATTACHED();

            break;
#endif

        default:

            return _TYPE_C_UNATTACH;

            break;
    }
}

//--------------------------------------------------
// Description  : Return Port Controller Alt Mode Ready Status to SysTypeC
// Input Value  : None
// Output Value : CC Attach (_TRUE / _FALSE)
//--------------------------------------------------
EnumTypeCAltModeStatus ScalerTypeCGetAltModeReady(BYTE ucInputPort)
{
    switch(ucInputPort)
    {
#if(_D0_DP_TYPE_C_PORT_CTRL_TYPE == _PORT_CONTROLLER_EMBEDDED)
        case _D0_INPUT_PORT:

            if(ScalerTypeCRx0PdGetAltModeReady() == _TRUE)
            {
                return _TYPE_C_ALT_MODE_READY;
            }
            else
            {
                return _TYPE_C_ALT_MODE_NOT_READY;
            }

            break;
#endif

#if(_D1_DP_TYPE_C_PORT_CTRL_TYPE == _PORT_CONTROLLER_EMBEDDED)
        case _D1_INPUT_PORT:

            // return _TYPE_C_ALT_MODE_NOT_READY;

            break;
#endif

#if(_D6_DP_TYPE_C_PORT_CTRL_TYPE == _PORT_CONTROLLER_EMBEDDED)
        case _D6_INPUT_PORT:

            // return _TYPE_C_ALT_MODE_NOT_READY;

            break;
#endif

        default:

            return _FALSE;

            break;
    }
}

//--------------------------------------------------
// Description  : Return Port Controller Alt Mode Pin Assignment to SysTypeC
// Input Value  : None
// Output Value : _SUCCESS / _FAIL
//--------------------------------------------------
bit ScalerTypeCGetPinAssignment(BYTE ucInputPort, EnumTypeCPinCfgType *penumPinAssignment)
{
    switch(ucInputPort)
    {
#if(_D0_DP_TYPE_C_PORT_CTRL_TYPE == _PORT_CONTROLLER_EMBEDDED)
        case _D0_INPUT_PORT:

            *penumPinAssignment = ScalerTypeCRx0PdGetPinAssignment();
            return _SUCCESS;

            break;
#endif

#if(_D1_DP_TYPE_C_PORT_CTRL_TYPE == _PORT_CONTROLLER_EMBEDDED)
        case _D1_INPUT_PORT:

            // *penumPinAssignment = ScalerTypeCRx0PdGetD1PinAssignment();
            return _SUCCESS;

            break;
#endif

#if(_D6_DP_TYPE_C_PORT_CTRL_TYPE == _PORT_CONTROLLER_EMBEDDED)
        case _D6_INPUT_PORT:

            // *penumPinAssignment = ScalerTypeCRx0PdGetD6PinAssignment();
            return _SUCCESS;

            break;
#endif

        default:

            return _FAIL;

            break;
    }
}

//--------------------------------------------------
// Description  : Type C Reconnect Process
// Input Value  : ucInputPort
// Output Value : _SUCCESS / _FAIL
//--------------------------------------------------
bit ScalerTypeCReconnect(BYTE ucInputPort)
{
    switch(ucInputPort)
    {
#if(_D0_DP_TYPE_C_PORT_CTRL_TYPE == _PORT_CONTROLLER_EMBEDDED)
        case _D0_INPUT_PORT:

            SET_TYPEC_ERROR_RECOVERY();

            return _SUCCESS;

            break;
#endif

#if(_D1_DP_TYPE_C_PORT_CTRL_TYPE == _PORT_CONTROLLER_EMBEDDED)
        case _D1_INPUT_PORT:

            // SET_TYPEC_ERROR_RECOVERY();

            return _SUCCESS;

            break;
#endif

#if(_D6_DP_TYPE_C_PORT_CTRL_TYPE == _PORT_CONTROLLER_EMBEDDED)
        case _D6_INPUT_PORT:

            // SET_TYPEC_ERROR_RECOVERY();

            return _SUCCESS;

            break;
#endif

        default:

            return _FAIL;

            break;
    }
}

//--------------------------------------------------
// Description  : Type C Configuration Update
// Input Value  : ucInputPort
// Output Value : _SUCCESS / _FAIL
//--------------------------------------------------
bit ScalerTypeCUpdateConfig(BYTE ucInputPort)
{
    ucInputPort = ucInputPort;

    return _TRUE;
}

//--------------------------------------------------
// Description  : Type C Interrupt Process
// Input Value  : None
// Output Value : None
//--------------------------------------------------
void ScalerTypeCIntHandler_EXINT0(void) using 1
{
#if(_D0_DP_TYPE_C_PORT_CTRL_TYPE == _PORT_CONTROLLER_EMBEDDED)
    ScalerTypeCPowerIntHandler_EXINT0();
    ScalerTypeCRx0CcIntHandler_EXINT0();
    ScalerTypeCRx0PdIntHandler_EXINT0();

    if((ScalerTypeCRx0CcIntClrCheck_EXINT0() == _TRUE) && (ScalerTypeCRx0PdIntClrCheck_EXINT0() == _TRUE))
    {
        MCU_FD29_UFP_DPF_IRQ_STATUS |= _BIT7;
    }
#endif
}


//--------------------------------------------------
// Description  : Type C Power Switch Process
// Input Value  : enumPowerAction
// Output Value : None
//--------------------------------------------------
void ScalerTypeCPowerSwitch(EnumPowerAction enumPowerAction)
{
    enumPowerAction = enumPowerAction;

#if(_D0_DP_TYPE_C_PORT_CTRL_TYPE == _PORT_CONTROLLER_EMBEDDED)
    ScalerTypeCRx0CcPowerSwitch(enumPowerAction);
    ScalerTypeCRx0PdPowerSwitch(enumPowerAction);
#endif
}

//--------------------------------------------------
// Description  : Type C DC Off Option
// Input Value  : None
// Output Value : DC Off Option
//--------------------------------------------------
BYTE ScalerTypeCGetDcOffOption(void)
{
    return _TYPEC_DC_OFF_OPTION;
}

#endif // End of #if((_DP_TYPE_C_SUPPORT == _ON) && (_PORT_CONTROLLER_EMBEDDED_SUPPORT == _ON))
